package com.bsj.pay.controller.merchant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;
import com.bsj.pay.common.ExcelData;
import com.bsj.pay.model.MerchantBean;
import com.bsj.pay.model.OrderBean;
import com.bsj.pay.service.OrderService;
import com.bsj.pay.util.ResultUtil;
import com.bsj.pay.util.date.DateTimeUtil;
import com.bsj.pay.util.excel.ExportExcelUtil;
import com.github.pagehelper.PageInfo;

@RequestMapping("/merchant/order")
@Controller("merchantOrderController")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping(value = "/orderExport.html")
	public void  orderExport(HttpServletRequest req, HttpServletResponse res,OrderBean order) throws Exception {
		OrderBean queryModel=new OrderBean();
		MerchantBean muser=(MerchantBean) req.getSession().getAttribute("muser");
		queryModel.setMerchant_id(muser.getId());
		if(order.getId()!=null)queryModel.setId(order.getId());
		if(!StringUtils.isEmpty(order.getPay_type()))queryModel.setPay_type(order.getPay_type());
		if(!StringUtils.isEmpty(order.getMerchant_order_no()))queryModel.setMerchant_order_no(order.getMerchant_order_no());
		if(order.getStatus()!=null)queryModel.setStatus(order.getStatus());
		if(order.getNotify_status()!=null)queryModel.setNotify_status(order.getNotify_status());
		List<OrderBean> lists=orderService.queryList(queryModel);
		 ExcelData data = new ExcelData();
	     data.setName("订单列表");
	     List<String> titles = new ArrayList<String>();
	     titles.add("平台订单号");
	     titles.add("商户订单号");
	     titles.add("订单金额");
	     titles.add("支付类型");
	     titles.add("下单时间");
	     titles.add("支付时间");
	     titles.add("订单状态");
	     data.setTitles(titles);
	     List<List<Object>> rows = new ArrayList<List<Object>>();
	     List<Object> row =null;
	     for (OrderBean o : lists) {
	    	 row = new ArrayList<Object>();
	    	 row.add(o.getId());
	    	 row.add(o.getMerchant_order_no());
	    	 row.add(o.getAmount());
	    	 row.add(o.getPay_type());
	    	 row.add(DateTimeUtil.unixTime2DateStr(o.getCreate_time()));
	    	 row.add(o.getPay_time()==null?"":DateTimeUtil.unixTime2DateStr(o.getPay_time()));
	    	 row.add(o.getStatus()==1?"已支付":"未支付");
	    	 rows.add(row);
	     }
	     data.setRows(rows);
	     ExportExcelUtil.exportExcel(res,"订单列表.xlsx",data);
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/list.html")
	public Map list(HttpServletRequest req, HttpServletResponse res,int page,int limit,OrderBean order) {
		MerchantBean muser=(MerchantBean) req.getSession().getAttribute("muser");
		OrderBean queryModel=new OrderBean();
		queryModel.setMerchant_id(muser.getId());
		if(order.getId()!=null)queryModel.setId(order.getId());
		if(!StringUtils.isEmpty(order.getPay_type()))queryModel.setPay_type(order.getPay_type());
		if(!StringUtils.isEmpty(order.getMerchant_order_no()))queryModel.setMerchant_order_no(order.getMerchant_order_no());
		if(order.getStatus()!=null)queryModel.setStatus(order.getStatus());
		if(order.getNotify_status()!=null)queryModel.setNotify_status(order.getNotify_status());
		PageInfo<OrderBean> pageInfo=orderService.pageList(queryModel, page, limit);
		return ResultUtil.getQuerySuccessResult(pageInfo);
	}
	
	
	@RequestMapping("/listPage.html")
    public ModelAndView listPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/order/list");
		return mav;
    }
	
	@ResponseBody
	@RequestMapping(value = "/dayFinanceList.html")
	public Map dayFinanceList(HttpServletRequest req, HttpServletResponse res,int page,int limit) {
		MerchantBean muser=(MerchantBean) req.getSession().getAttribute("muser");
		OrderBean queryModel=new OrderBean();
		queryModel.setMerchant_id(muser.getId());
		PageInfo<OrderBean> pageInfo=orderService.pageDayFinanceList(queryModel, page, limit);
		return ResultUtil.getQuerySuccessResult(pageInfo);
	}
	
	
	@RequestMapping("/dayFinanceListPage.html")
    public ModelAndView dayFinanceListPage(HttpServletRequest request,HttpServletResponse response,ModelAndView mav) {
		mav.setViewName("merchant/order/dayFinanceList");
		return mav;
    }
}
